package cn.iocoder.yudao.module.peizhen.controller.app.buiessorder;

import cn.iocoder.yudao.module.pay.api.notify.dto.PayOrderNotifyReqDTO;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.paitent.PaitentDO;
import cn.iocoder.yudao.module.peizhen.enums.BuiessOrderStatusEnum;
import cn.iocoder.yudao.module.peizhen.service.paitent.PaitentService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.peizhen.controller.app.buiessorder.vo.*;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.buiessorder.BuiessOrderDO;
import cn.iocoder.yudao.module.peizhen.service.buiessorder.BuiessOrderService;

@Tag(name = "用户 APP - 订单")
@RestController
@RequestMapping("/peizhen/order")
@Validated
public class AppBuiessOrderController {

    @Resource
    private BuiessOrderService buiessOrderService;
    @Resource
    private PaitentService paitentService;

    @PostMapping("/create")
    @Operation(summary = "创建订单")
    public CommonResult<Long> createOrder(@Valid @RequestBody AppOrderSaveReqVO createReqVO) {
        return success(buiessOrderService.createOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单")
    public CommonResult<Boolean> updateOrder(@Valid @RequestBody AppOrderSaveReqVO updateReqVO) {
        buiessOrderService.updateOrder(updateReqVO);
        return success(true);
    }

    @PutMapping("/cancel")
    @Operation(summary = "取消订单")
    public CommonResult<Boolean> cancelOrder(@Valid @RequestBody AppOrderSaveReqVO updateReqVO) {
        buiessOrderService.cancelOrderById(updateReqVO.getId());

        return success(true);
    }

    @PutMapping("/compleate")
    @Operation(summary = "完成订单")
    public CommonResult<Boolean> compleateOrder(@Valid @RequestBody AppOrderSaveReqVO updateReqVO) {
        buiessOrderService.compleate(updateReqVO.getId());
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除订单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteOrder(@RequestParam("id") Long id) {
        buiessOrderService.deleteOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppOrderRespVO> getOrder(@RequestParam("id") Long id) {
        BuiessOrderDO order = buiessOrderService.getOrder(id);
        AppOrderRespVO appOrderRespVO = BeanUtils.toBean(order, AppOrderRespVO.class);
        PaitentDO paitent = paitentService.getPaitent(appOrderRespVO.getPaitentId());
        //获取身份证号码
        appOrderRespVO.setPaitenCardNum(paitent.getCardNum());
        appOrderRespVO.setPaitentRelation(paitent.getRelation());
        return success(appOrderRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单分页")
    public CommonResult<PageResult<AppOrderRespVO>> getOrderPage(@Valid AppBuiessOrderPageReqVO pageReqVO) {
        PageResult<BuiessOrderDO> pageResult = buiessOrderService.getOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderExcel(@Valid AppBuiessOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BuiessOrderDO> list = buiessOrderService.getOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单.xls", "数据", AppOrderRespVO.class,
                        BeanUtils.toBean(list, AppOrderRespVO.class));
    }

    @PostMapping("/update-paid")
    @Operation(summary = "更新订单为已支付") // 由 pay-module 支付服务，进行回调，可见 PayNotifyJob
    @PermitAll // 无需登录，安全由 PayDemoOrderService 内部校验实现
    public CommonResult<Boolean> updateDemoOrderPaid(@RequestBody PayOrderNotifyReqDTO notifyReqDTO) {
        buiessOrderService.updateOrderPaid(Long.valueOf(notifyReqDTO.getMerchantOrderId()),
                notifyReqDTO.getPayOrderId());
        return success(true);
    }

}